"use strict";
var Const = require('./const');

function login() {
	return new Promise(function (resolve, reject) {
		wx.login({success: resolve, fail: reject});
	});
}

function getUserInfo(params) {
	return new Promise(function (resolve, reject) {
		wx.getUserInfo(Object.assign({}, params, {success: resolve, fail: reject}));
	});
}

function setStorage(key, value) {
	return new Promise(function (resolve, reject) {
		wx.setStorage({key: key, data: value, success: resolve, fail: reject});
	});
}

function getStorage(key) {
	return new Promise(function (resolve, reject) {
		wx.getStorage({key: key, success: resolve, fail: reject});
	});
}

function getLocation(type) {
	return new Promise(function (resolve, reject) {
		wx.getLocation({type: type, success: resolve, fail: reject});
	});
}

function checkSession() {
	return new Promise(function (resolve, reject) {
		wx.checkSession({success: resolve, fail: reject});
	});
}

function uploadFile(url, name, filePath, params) {
	return new Promise(function (resolve, reject) {
		wx.uploadFile({
			url: url,
			filePath: filePath,
			name: name,
			formData: Object.assign({}, params),
			success: resolve,
			fail: reject
		})
	});
}

function payDeposit() {
	return new Promise(function (resolve, reject) {
		wx.request({
			url: 'https://tianheng.xlxiot.com/wx/deposit/prePay',
			data: {
				ticket: getApp().data.token
			},
			success: function (response) {
				var resultMessage = 'SYSTEM ERROR';
				if (response.statusCode == 200) {
					if (response.data.hasOwnProperty('code')) {
						var code = response.data.code;
						if (code == Const.SUCCESS.DEFAULT) {
							var _data = response.data.data;
							// 发起微信支付
							wx.requestPayment({
								'timeStamp': _data.timeStamp,
								'nonceStr': _data.nonceStr,
								'package': _data.package,
								'signType': _data.signType,
								'paySign': _data.paySign,
								'success': resolve,
								'fail': reject
							});
							console.log(_data);
							console.log('-----支付结束-----');
						} else {
							reject(response.data);
						}
					}
				} else {
					resultMessage = Const.ERROR_MSG.ERROR_NETWORK
					reject({code: 0, message: resultMessage});
				}
			}
		})
	});
}

function payRecharge(total) {
	return new Promise(function (resolve, reject) {
		wx.request({
			url: 'https://tianheng.xlxiot.com/wx/recharge/prePay',
			data: {
				total: total,
				ticket: getApp().data.token
			},
			success: function (response) {
				var resultMessage = 'SYSTEM ERROR';
				if (response.statusCode == 200) {
					if (response.data.hasOwnProperty('code')) {
						var code = response.data.code;
						if (code == Const.SUCCESS.DEFAULT) {
							var _data = response.data.data;
							getApp().data.tempOrderId = _data.orderId;
							// 发起微信支付
							wx.requestPayment({
								'timeStamp': _data.timeStamp,
								'nonceStr': _data.nonceStr,
								'package': _data.package,
								'signType': _data.signType,
								'paySign': _data.paySign,
								'success': resolve,
								'fail': reject
							});
							console.log(_data);
							console.log('-----支付结束-----');
						} else {
							reject(response.data);
						}
					}
				} else {
					resultMessage = Const.ERROR_MSG.ERROR_NETWORK
					reject({code: 0, message: resultMessage});
				}
			}
		})
	});
}

module.exports = {
	login: login,
	getUserInfo: getUserInfo,
	setStorage: setStorage,
	getStorage: getStorage,
	getLocation: getLocation,
	checkSession: checkSession,
	uploadFile: uploadFile,
	original: wx,
	payDeposit: payDeposit,
	payRecharge: payRecharge
};